//获取链接
function getUrl() {
	// switch (__wxConfig.envVersion) {
	// 	case 'develop':
	// 		return 'http://localhost:11020/server/api'; //测试版
	// 		break;
	// 	case 'trial':
	// 		return 'http://localhost:11000/server/api'; //体验版
	// 		break;
	// 	case 'release':
	// 		return 'http://localhost:11000/server/api'; //线上
	// 		break;
	// 	default:
	// 		return 'http://localhost:11000/server/api';
	// }
	return 'http://localhost:11020/server/api';
}

//请求
function request(url, options) {
	console.info("请求参数", url, options)
	//是否展示【加载中...】
	if (options.isToast) {
		uni.$common.toastLoading()
	}
	return new Promise((resolve, reject) => {
		console.info("进行网络请求", url, options.data)
		uni.request({
			url: getUrl() + `${url}`,
			method: options.method,
			data: options.data,
			header: {
				'authorization': "Bearer_" + uni.getStorageSync("token"),
				'client': uni.getSystemInfoSync().uniPlatform,
			},
			success: (res) => {
				console.info("网络请求成功", url, res)
				if (res.statusCode == 200) {
					successOperate(url, options, res, resolve, reject)
				} else {
					errorOperate(url,res,reject)
				}
			},
			fail: (error) => {
				errorOperate(url,error,reject)
			}
		})
	})
}

// 上传文件
function httpUpFile(url, options, config) {
	options = setOptions('POST','UPLOAD_FILE',options,config)
	console.info("上传文件参数", url, options)
	if (options.isToast) {
		uni.$common.toastLoading()
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: getUrl() + `${url}`,
			filePath: options.data.file,
			name: 'file',
			formData: {
				rootPath: "app/" + options.data.rootPath
			},
			header: {
				'authorization': "Bearer_" + uni.getStorageSync("token"),
				'client': uni.getSystemInfoSync().uniPlatform
			},
			success: (res) => {
				console.info("网络请求成功", url, res)
				if (res.statusCode == 200) {
					res.data = JSON.parse(res.data)
					successOperate(url, options, res, resolve, reject)
				} else {
					errorOperate(url,res,reject)
				}
			},
			fail: (error) => {
				errorOperate(url,error,reject)
			}
		});
	})
}

//请求成功的处理
function successOperate(url, options, requestData, resolve, reject) {
	if (options.isToast) {
		uni.$common.toastHide()
	}

	if (requestData.data.code == 200) {
		resolve(requestData.data.result)
	} else if (requestData.data.code == 401 && !options.isLoginRequest) {
		uni.removeStorageSync("token")
		//其它接口需要重新登录
		uni.$login.login((result) => {
			if (result) {
				console.info("登录成功")
				if (options.type == "UPLOAD_FILE") {
					resolve(httpUpFile(url, options))
				} else {
					resolve(request(url, options))
				}
			}
		})
	} else if (requestData.data.code == 401 && options.isLoginRequest) {
		//登录接口登录失败
		if (requestData.data.message) {
			uni.$common.modalWarin(requestData.data.message)
		}
		reject(requestData.data)
	} else {
		if (requestData.data.message) {
			uni.$common.modalWarin(requestData.data.message)
		}
		reject(requestData.data)
	}
}

//请求异常的处理
function errorOperate(url,error, reject){
	console.error("网络请求失败", url, error)
	uni.$common.toastHide()
	uni.$common.modalError("请求失败，请检查网络或联系管理员." + error.errMsg)
	reject(error.data)
}
//查询
function httpGet(url, options, config) {
	return request(url,setOptions('GET','REQUEST',options,config))
}

//添加/修改
function httpPost(url, options, config) {
	return request(url,setOptions('POST','REQUEST',options,config))
}

//删除
function httpDelete(url, options, config) {
	return request(url,setOptions('DELETE','REQUEST',options,config))
}

/**
 * 设置请求参数
 * @param {Object} method 请求方式
 * @param {Object} type 成功处理类型。正常请求、上传文件
 * @param {Object} options	请求参数
 * @param {Object} config	请求配置
 */
function setOptions(method,type,options,config){
	if(!options){
		options = {}
	}
	if(config){
		if(!config.hasOwnProperty("isToast")){
			config.isToast = true
		}
		if(!config.hasOwnProperty("isLoginRequest")){
			config.isLoginRequest = false
		}
	}else{
		config = {
			isToast: true,
			isLoginRequest:false
		}
	}
	
	return {
		method: method,
		type: type,
		data: options,
		isToast: config.isToast,
		isLoginRequest:config.isLoginRequest
	}
}

export default {
	getUrl: getUrl,
	get: httpGet,
	post: httpPost,
	delete: httpDelete,
	uploadFile: httpUpFile
}
